<template>
  <div class="global-container">
    <div class="search">
      <el-form ref="form" :model="searchData" inline>
        <el-form-item label="申请人身份证" prop="appPer">
          <el-input v-model="searchData.holderCode"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <Atable :tableData="tableData" :tableModel="tableModel">
      <template #actions="{ row }">
        <el-button @click="view(row)" type="primary" size="small"
        >下载</el-button
        >
      </template>
    </Atable>
    <PageSize
        ref="pageSize"
        :total="pageParams.total"
        :pageNum="pageParams.pageNo"
        :pageSize="pageParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    ></PageSize>
  </div>
</template>
<script>
import Atable from '@/components/Table/Atable';
import PageSize from '@/components/PageSize/PageSize';
import businessApi from '@/api/business';
import dayjs from "dayjs";


export default {
  components: { Atable, PageSize },
  data() {
    return {
      previewVisible: false,
      previewUrl: '',
      pageParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      searchData: {
        certCode: null,
        appPer: null,
        employUnit: null,
        isValid: null,
        certTimeRange: null,
        // certStatus 1:已发证
        certStatus: 1
      },
      bussTypeMap: {
        '1': '二级造价工程师（水利工程）初始注册',
        '2': '二级造价工程师（水利工程）延续注册',
        '3': '二级造价工程师（水利工程）变更注册',
        '4': '二级造价工程师（水利工程）注销注册',
        '5': '水利工程质量检测单位资质认定（乙级）-常规审批制',
        '6': '水利工程质量检测单位资质认定（乙级）认定-初次申领',
        '7': '水利工程质量检测单位资质认定（乙级）认定-延续申请',
        '8': '水利工程质量检测单位资质认定（乙级）认定-变更申请',
        '9': '水利工程质量检测单位资质认定（乙级）认定-注销申请',
        'a': '三类人员安全生产考核合格证书-初次申领',
        'b': '三类人员安全生产考核合格证书-延续申请',
        'c': '三类人员安全生产考核合格证书-变更申请',
        'd': '三类人员安全生产考核合格证书-注销申请',
      },
      certTypeMap: {
        '1': '二级造价工程师（水利工程）',
        '2': '水利工程质量检测单位资质认定（乙级）',
        '3': '三类人员安全生产考核合格证书'
      },
      tableData: [],
      tableModel: [
        {
          prop: 'certCode',
          label: '证书编号',
        },
        {
          prop: 'appPer',
          label: '申请人',
          // render: (row) => {
          //   return row.appPer || row.appUnit;
          // },
        },
        {
          prop: 'paperCode',
          label: '身份证号',
        },
        {
          prop: 'paperCode',
          label: '身份证号',
        },
        {
          prop: 'safeType',
          label: '申请人类型',
        },
        {
          prop: 'employUnit',
          label: '所在单位'
        },
        {
          prop: 'effRange',
          label: '有效期',
          render: (row) => {
            return `${row.effStartTime}至${row.effEndTime}`
          },
        },
        {
          prop: 'certTime',
          label: '发证日期',
        }
      ],
    };
  },
  mounted() {
    this.getTable();
  },
  computed: {
    userType() {
      return this.$store.state.user.userInfo.userType;
    },
    userCertType() {
      if ((this.userType.includes('5') && this.userType.includes('6')) || this.userType.includes('7')) {
        return undefined;
      } else if (this.userType.includes('5')) {
        return 1;
      } else if (this.userType.includes('6')) {
        return 2;
      }
      return undefined;
    }
  },
  methods: {
    async getTable(searching=false) {
      this.tableData = [];
      if (searching) {
        this.pageParams.pageNo = 1;
      }
      let params = {
        ...this.pageParams,
        ...this.searchData,

        certType: this.userCertType,
      }
      if (this.searchData.certTimeRange && this.searchData.certTimeRange.length > 0) {
        params.certStartTime = this.searchData.certTimeRange[0];
        params.certEndTime = this.searchData.certTimeRange[1];
      }
      await businessApi.getCertsFromMwr(params)
            .then((res) => {
              if (res && res.status==200) {
                const {data} = res;
                const certList = data?.data;
                if (certList && certList.length) {
                  this.tableData = certList.map((item) => {
                    const {REG_INFO, ISSU_DATE,
                      VAL_PER_FROM, VAL_PER_TO,
                      HOLDER_INFO,
                    } = item;
                    return {
                      ...item,
                      certCode: item.CERT_NUM_CODE,
                      certType: '3',
                      appPer: HOLDER_INFO.HOLDER_NM,
                      paperCode: HOLDER_INFO.HOLDER_CODE,
                      employUnit: REG_INFO.ENT_NAME,
                      safeType: REG_INFO.SAFE_TYPE,
                      effStartTime: dayjs(VAL_PER_FROM).format('YYYY-MM-DD'),
                      effEndTime: dayjs(VAL_PER_TO).format('YYYY-MM-DD'),
                      certTime: dayjs(ISSU_DATE).format('YYYY-MM-DD'),
                    };
                  });
                }
                this.pageParams.total = certList.length;
              }
            }).finally(() => {
              this.$emit('loading', false);
            });
          },
    reset() {
      this.searchData = {
        // certStatus 1:已发证
        certStatus: 1
      };
      this.pageParams.pageNo = 1;
      this.getTable();
    },
    view(row) {
      businessApi.getCertificateById({
        id: row.id
      }).then(res => {
        // const { fileUri } = res.certFile;
        if (res.certFile && res.certFile.fileUri) {
          // download ofd
          window.location.href = res.certFile.fileUri;
          // this.previewUrl = fileUri;
          // this.previewVisible = true;
        } else {
          this.$message.error('暂无证书');
        }
      })

    },
    handleSizeChange(val) {
      this.pageParams.pageSize = val;
      this.getTable();
    },
    handleCurrentChange(val) {
      this.pageParams.pageNo = val;
      this.getTable();
    },
  }
};
</script>
<style lang="scss" scoped></style>
